package user

import (
	"common/logging"
	"iot/iot_app_server/app/util"
	"iot/iot_app_server/app/db"
	"net/http"
	"time"
)

type DeviceInfo struct {
	Deviceid string      `json:"deviceid,omitempty"`
	Name string      `json:"name,omitempty"`
	Online bool      `json:"online,omitempty"`
	BindTime string      `json:"bind_time,omitempty"`
	Info     interface{} `json:"info,omitempty"`
}

func HandleGetDeviceList(w http.ResponseWriter, req *http.Request) {
	//解决跨域问题
	sTime := time.Now()
	w.Header().Add("Access-Control-Allow-Origin", "*")
	rmap := make(map[string]interface{})
	//time.Sleep(time.Second*5)
	payloadInfo := &util.PayloadInfo{}
	_, err := util.GetReqBody(req, payloadInfo)
	if err != nil {
		w.Write(util.LogGetResponseString(req, sTime, payloadInfo.Msgid, util.ERR_CODE_PARA_ERROR, rmap))
		logging.Error("HandleGetDeviceList para error 1,GetReqBody failed,request=%v", req.Form)
		return
	}

	//userid := payloadInfo.Userid
	device1 := DeviceInfo{
		Deviceid: "1111",
		Name: "空调",
		Info:     "空调1111",
		BindTime: "2017-05-01 14:57:13",
		//Online: true,
	}
	device1.Online,_  = db.GetDeviceStatus(device1.Deviceid)
	device2 := DeviceInfo{
		Deviceid: "testuser",
		Name: "洗衣机",
		Info:     "洗衣机1112",
		BindTime: "2017-05-02 11:57:13",
		//Online: true,
	}
	device2.Online,_ =  db.GetDeviceStatus(device2.Deviceid)
	device3 := DeviceInfo{
		Deviceid: "testuser001",
		Name: "冰箱",
		Info:     "冰箱001",
		BindTime: "2017-05-04 09:23:42",
		//Online: false,
	}
	device3.Online,_ =  db.GetDeviceStatus(device3.Deviceid)
	
	device4 := DeviceInfo{
		Deviceid: "testuser002",
		Name: "冰箱",
		Info:     "冰箱002",
		BindTime: "2017-05-04 09:23:42",
		//Online: false,
	}
	device4.Online,_ =  db.GetDeviceStatus(device4.Deviceid)
	
	device5 := DeviceInfo{
		Deviceid: "testuser003",
		Name: "冰箱",
		Info:     "冰箱003",
		BindTime: "2017-05-04 09:23:42",
		//Online: false,
	}
	device5.Online,_ =  db.GetDeviceStatus(device5.Deviceid)
	
	device6 := DeviceInfo{
		Deviceid: "testuser004",
		Name: "冰箱",
		Info:     "冰箱004",
		BindTime: "2017-05-04 09:23:42",
		//Online: false,
	}
	device6.Online,_ =  db.GetDeviceStatus(device6.Deviceid)
	
	
	device_list := []DeviceInfo{device1, device2,device3,device4,device5,device6}

	rmap["device_list"] = device_list

	w.Write(util.LogGetResponseString(req, sTime, payloadInfo.Msgid, util.ERR_CODE_SUCCESS, rmap))
	logging.Debug("HandleGetSnByDeviceid ok ,request=%v", req.Form)
}
